package com.saer.server.rpc;

import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.saer.client.RemoteWebService;
import com.saer.client.constants.ClientConstants;
import com.saer.client.constants.SaerProperties;
import com.saer.client.dto.diagnostico.VisitaDto;
import com.saer.client.dto.network.MsgDto;
import com.saer.server.service.DiagnosticoService;
import com.saer.server.service.NetworkService;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class RemoteWebServiceImpl extends RemoteServiceServlet implements RemoteWebService {
	
	private ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "database-context.xml", "hibernate-context.xml", "service-context.xml" });

	private Logger log = LoggerFactory.getLogger(RemoteWebServiceImpl.class);
	
	public List<VisitaDto> getVisitas() {
		return ((DiagnosticoService) context.getBean("diagnosticoService")).getVisitas();
	}
	
	public List<VisitaDto> getVisitas(int centroMedicoType, int centroMedicoId) {
		if (centroMedicoType == ClientConstants.CAP) {
			return ((DiagnosticoService) context.getBean("diagnosticoService")).getVisitasByCap(centroMedicoId);
		} else {
			return ((DiagnosticoService) context.getBean("diagnosticoService")).getVisitasByHospital(centroMedicoId);
		}
	}
	
	public List<VisitaDto> getVisitasByHospital(int hospitalId){
		return ((DiagnosticoService) context.getBean("diagnosticoService")).getVisitasByHospital(hospitalId);
	}
	
	public MsgDto getNewVisita(int clientId, int centroMedicoType, int centroMedicoId) {
		return ((NetworkService) context.getBean("networkService")).getNewVisita(clientId, centroMedicoType, centroMedicoId);
	}
	
	public MsgDto endPoll(int clientId, int centroMedicoType, int centroMedicoId) {
		return ((NetworkService) context.getBean("networkService")).endPoll(clientId, centroMedicoType, centroMedicoId);
	}
	
	public void esperaVisita() {
		log.info("Esperando Visitas");
		((NetworkService) context.getBean("networkService")).esperaVisita();
	}
	
	public MsgDto sendToChacra(int visitaId, int capId, String text){
		return ((NetworkService) context.getBean("networkService")).sendToChacra(visitaId, capId, text);
	}
	
	public SaerProperties getSaerProperties() {
		return (SaerProperties) context.getBean("saerProperties");
	}
	
	public String exportListGrid(String[][] data) {
		UUID uniqueId = UUID.randomUUID();
		
		getThreadLocalRequest().getSession().setAttribute(uniqueId.toString(), data);
	    
		return uniqueId.toString();
	}
	
	public void ping() {
		return;
	}

	public void updateStatusVisita(int id, int status) {
		((DiagnosticoService) context.getBean("diagnosticoService")).updateStatusVisita(id, status);
	}

}
